package com.sskj.lib.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.R;

public class TipUtil {

    public static MaterialDialog getTip(Context context, String content, OnTipSure onTipSure) {
        return getTip(context, content, App.INSTANCE.getString(R.string.lib_tipUtil1_j), onTipSure);
    }

    public static MaterialDialog getTip(Context context, String content, boolean isCancel, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j))
                .content(content)
                .cancelable(false)
                .autoDismiss(false)
                .positiveText(App.INSTANCE.getString(R.string.lib_tipUtil1_j))
//                .negativeText("")
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static MaterialDialog getTip(Context context, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j))
                .content(content)
                .positiveText(sureMsg)
                .negativeText(App.INSTANCE.getString(R.string.lib_tipUtil3_j))
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static MaterialDialog getTip(Context context, String content, String sureMsg, OnTipSure2 onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j))
                .content(content)
                .positiveText(sureMsg)
                .negativeText(App.INSTANCE.getString(R.string.lib_tipUtil3_j))
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                        onTipSure.onCancel();
                    }
                }).build();
    }

    public static MaterialDialog getTip(Context context, String title, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .positiveText(sureMsg)
                .negativeText(App.INSTANCE.getString(R.string.lib_tipUtil3_j))
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static void showGonggao(Context context, String content) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.lib_dialog_gonggao, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        ((TextView) customeView.findViewById(R.id.tvContent)).setText(content);
        ClickUtil.click(customeView.findViewById(R.id.btSure), () -> {
            dialog.dismiss();
        });
    }


    public static MaterialDialog getExitTip(Activity activity, String content, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_exit, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        TextView tvContent = customView.findViewById(R.id.tvContent);
        tvContent.setText(content);
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.tvSure), () -> {
            onTipSure.onSure();
        });
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        return dialog;
    }

    public static MaterialDialog getSureTip(Activity activity, String title, String content, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        tvContent.setText(content);
        tvTitle.setText(title);
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.btSure), () -> {
            onTipSure.onSure();
        });
        return dialog;
    }

    public static MaterialDialog getSureTip(Activity activity, String content, OnTipSure onTipSure) {
        return getSureTip(activity, App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j), content, onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String content, OnTipSure onTipSure) {
        return getSureCancelTip(activity, App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j), content, onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String title, String content, OnTipSure onTipSure) {
        return getSureCancelTip(activity, title, content, App.INSTANCE.getString(R.string.lib_tipUtil1_j), onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String title, String content, String sure, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure_cancel, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup)customView.getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup)customView.getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup)customView.getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup)customView.getParent().getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup)customView.getParent().getParent().getParent().getParent().getParent()).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        TextView tvSure = customView.findViewById(R.id.tvSure);
        TextView btSure = customView.findViewById(R.id.btSure);
        ImageView ivClose = customView.findViewById(R.id.ivClose);
        tvSure.setText(sure);
        btSure.setText(sure);
        tvTitle.setText(title);
        tvContent.setText(content);
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.tvSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.btSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.ivClose), () -> {
            dialog.dismiss();
        });
        return dialog;
    }

    public interface OnTipSure {
        void onSure();

    }

    public interface OnTipSure2 {
        void onSure();

        void onCancel();
    }
}
